import axios from 'axios';

const req = axios.create();
req.defaults.baseURL='http://127.0.0.1:8888/api/private/v1';
req.defaults.timeout = 3000;
function ajax({method='get',url,params={},headtype=''}) {
    if(url!="/login"){
        req.defaults.headers['Authorization']=window.sessionStorage.getItem('token')
    }
    if(method.toLowerCase()=='get'){
        if(headtype=='form'){
            req.defaults.headers['content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
        }
        url = url + '/' + params.id;
    }
    return new Promise((resolve,reject)=>{
        req({
            method,
            url,
        }).then(res=>{
            resolve(res)
        }).catch(err=>{
            reject(err)
        })
    })
}
export default ajax